﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Com.Aote.ObjectTools;
using Com.Aote.Marks;

namespace s2.Pages
{

    public class WrapPanel : Panel
    {
        private static readonly DependencyProperty WrapPanelAttachedDataProperty =
           DependencyProperty.RegisterAttached("WrapPanelAttachedDataProperty", typeof(WrapPanelAttachedData), typeof(WrapPanel), null);

        private readonly List<double> rowHeights = new List<double>();

        protected override Size MeasureOverride(Size availableSize)
        {
            foreach (FrameworkElement child in this.Children)
            {
                child.Measure(availableSize);
            }

            double rowHeight = 0;
            int row = 0;
            this.rowHeights.Clear();

            Size desiredSize = Size.Empty;

            Point nextChildPosition = new Point(0, 0);

            foreach (FrameworkElement child in this.Children)
            {
                if (nextChildPosition.X + child.DesiredSize.Width > availableSize.Width)
                {
                    this.rowHeights.Add(rowHeight);
                    ++row;

                    nextChildPosition.X = 0;
                    nextChildPosition.Y += rowHeight;
                    rowHeight = 0;
                }

                WrapPanelAttachedData data = GetWrapPanelAttachedData(child);

                if (data.TargetPosition != nextChildPosition)
                {
                    data.TargetPosition = nextChildPosition;
                    data.Row = row;
                }

                desiredSize.Width = Math.Max(desiredSize.Width, nextChildPosition.X + child.DesiredSize.Width);
                desiredSize.Height = Math.Max(desiredSize.Height, nextChildPosition.Y + child.DesiredSize.Height);

                rowHeight = Math.Max(rowHeight, child.DesiredSize.Height);

                nextChildPosition.X += child.DesiredSize.Width;
            }

            this.rowHeights.Add(rowHeight);

            return desiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            foreach (UIElement child in this.Children)
            {
                WrapPanelAttachedData data = GetWrapPanelAttachedData(child);
                child.Arrange(new Rect(data.TargetPosition.X, data.TargetPosition.Y, child.DesiredSize.Width, this.rowHeights[data.Row]));
            }

            return finalSize;
        }

        private static WrapPanelAttachedData GetWrapPanelAttachedData(DependencyObject obj)
        {
            object value = obj.GetValue(WrapPanelAttachedDataProperty);

            if (value == null)
            {
                WrapPanelAttachedData data = new WrapPanelAttachedData();
                SetAnimatedWrapPanelAttachedData(obj, data);
                return data;
            }

            return (WrapPanelAttachedData)value;
        }

        private static void SetAnimatedWrapPanelAttachedData(DependencyObject obj, WrapPanelAttachedData value)
        {
            obj.SetValue(WrapPanelAttachedDataProperty, value);
        }

        private class WrapPanelAttachedData
        {
            public static readonly Point Unset = new Point(-1, -1);

            public WrapPanelAttachedData()
            {
                this.TargetPosition = Unset;
            }

            public Point TargetPosition { get; set; }

            public int Row { get; set; }
        }
    }

    public partial class DashBoard : Page
    {
        public DashBoard()
        {
            InitializeComponent();
        }


        private void BtnListen_Click(object sender, RoutedEventArgs e)
        {
            if(channelListened.Text == null || channelListened.Text.Length==0)
            {
                MessageBox.Show("请选择被监听坐席。");
                return;
            }

            AoPhone aPhone = thePhone.DataContext as AoPhone;
            if (aPhone.LineNum == channelListened.Text)
            {
                MessageBox.Show("自己不能监听自己。");
                return;
            }

            aPhone.ListenTo(channelListened.Text);
        }

        private void personListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (personListBox.SelectedItem == null)
                return;
            else
            {
                GeneralObject go = personListBox.SelectedItem as GeneralObject;
                channelListened.Text = go.GetPropertyValue("aChannel").ToString();
            }
        }

    }
}
